#include <stdio.h>
#include <stdlib.h>

typedef struct __subject {
    char sno[20];
    char name[20];
    float chinese;
    float math;
    float english;
} subject;

int main() {
    subject subj[5];
    int i;

    // 输入 5 个学生的信息
    for (i = 0; i < 5; i++) {
        printf("请输入第%d个学生的信息：\n", i + 1);
        printf("学号：");
        scanf("%s", subj[i].sno);
        printf("姓名：");
        scanf("%s", subj[i].name);
        printf("语文成绩：");
        scanf("%f", &subj[i].chinese);
        printf("数学成绩：");
        scanf("%f", &subj[i].math);
        printf("英语成绩：");
        scanf("%f", &subj[i].english);
    }

    // 写入文件
    FILE *fp = fopen("data", "wb");
    if (fp == NULL) {
        perror("无法打开文件进行写入");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i < 5; i++) {
        if (fwrite(&subj[i], sizeof(subject), 1, fp) != 1) {
            perror("写入文件失败");
            exit(EXIT_FAILURE);
        }
    }
    fclose(fp);

    // 读取并显示第 1、3、5 条记录
    fp = fopen("data", "rb");
    if (fp == NULL) {
        perror("无法打开文件进行读取");
        exit(EXIT_FAILURE);
    }

    subject s;

    // 第一条记录
    fseek(fp, 0 * sizeof(subject), SEEK_SET);
    fread(&s, sizeof(subject), 1, fp);
    printf("\n第一条记录：\n");
    printf("学号：%s\n", s.sno);
    printf("姓名：%s\n", s.name);
    printf("语文：%.1f\n", s.chinese);
    printf("数学：%.1f\n", s.math);
    printf("英语：%.1f\n", s.english);

    // 第三条记录
    fseek(fp, 2 * sizeof(subject), SEEK_SET);
    fread(&s, sizeof(subject), 1, fp);
    printf("\n第三条记录：\n");
    printf("学号：%s\n", s.sno);
    printf("姓名：%s\n", s.name);
    printf("语文：%.1f\n", s.chinese);
    printf("数学：%.1f\n", s.math);
    printf("英语：%.1f\n", s.english);

    // 第五条记录
    fseek(fp, 4 * sizeof(subject), SEEK_SET);
    fread(&s, sizeof(subject), 1, fp);
    printf("\n第五条记录：\n");
    printf("学号：%s\n", s.sno);
    printf("姓名：%s\n", s.name);
    printf("语文：%.1f\n", s.chinese);
    printf("数学：%.1f\n", s.math);
    printf("英语：%.1f\n", s.english);

    fclose(fp);
    return 0;
}
